//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Web;
using System.Xml;

namespace CommunityServer.Components {
    
	/// <summary>
    /// Summary description for Vote.
    /// </summary>
    public class PollSummary {

		#region Member variables

		string question	= "";
		string description	= "";
		Hashtable voters = new Hashtable();
		ArrayList answers = new ArrayList();
		CSContext csContext = CSContext.Current;
		DateTime expirationDate = DateTime.MinValue;
		Post post;

		#endregion

		#region Constructor
		 
		public PollSummary(Post post) 
		{
			this.post = post;

			if (post.PostType == PostContentType.Xml)
			{
				// TODO: Create an upgrade tool so this isn't neccessary :-)
				// Support old style polls

				XmlDocument dom = new XmlDocument();
				XmlNodeList choicesNodeList;

				// Load the dom
				//
				dom.LoadXml(post.Body);

				XmlNode voteOptions = dom.SelectSingleNode("VoteOptions");
				if( voteOptions != null ) 
				{
					if( voteOptions.Attributes["question"] != null )
						this.question = voteOptions.Attributes["question"].Value;

					if( voteOptions.Attributes["description"] != null )
						this.description = voteOptions.Attributes["description"].Value;

					// Bind to the Choices node and a Node Collection
					//
					choicesNodeList = voteOptions.ChildNodes;

					// Walk through each node in the Node List and add to Array List
					//
					int i = 0;
					foreach (XmlNode node in choicesNodeList)
						answers.Add(new PollItem(node.Name, node.InnerText, i++));
				}
			}
			else
			{
				this.question = this.post.PollTitle;
				this.description = this.post.PollDescription;
				this.answers = this.post.GetPollItems();
				this.expirationDate = this.post.PollExpirationDate;
			}
		}

		#endregion

		#region HasVoted

		public bool HasVoted (int userID) 
		{
			if (voters[userID] == null || GetPollItemByAnswerID((string) voters[userID]) == null)
				return false;

			return true;
		}

		public PollItem GetUserVote (int userID) 
		{
			if (!HasVoted(userID))
				return null;

			return GetPollItemByAnswerID((string) voters[userID]);
		}

		#endregion

		#region Public Methods

		public PollItem GetPollItemByAnswerID(string answerID)
		{
			foreach (PollItem item in this.Answers)
			{
				if (answerID == item.AnswerID)
					return item;
			}

			return null;
		}

		#endregion

		#region Public Properties

		public string Question 
		{
            get { return question; }
        }

		public  Hashtable Voters 
		{
			get { return voters; }
		}

        public ArrayList Answers 
		{
            get { return answers; }
        }

		public DateTime ExpirationDate
		{
			get { return expirationDate; }
		}

		public bool PollExpires
		{
			get { return ExpirationDate != DateTime.MinValue; }
		}

		public bool IsExpired
		{
			get { return PollExpires && ExpirationDate.Date < DateTime.Today; }
		}

		public int PostID 
		{
			get { return post.PostID; }
		}

		public string Description 
		{
			get { return description; }
		}

		#endregion

		#region Reporting Methods

		public PollVoteSet GetUserVotes(int pageIndex, int pageSize)
		{
			PollVoteSet pvs = new PollVoteSet();
			
			ArrayList users = new ArrayList(voters.Keys);
			users.Sort();

			int start = pageIndex * pageSize;
			int end = (pageIndex + 1) * pageSize;

			Hashtable validAnswers = new Hashtable();
			pvs.TotalRecords = 0;
			foreach (PollItem answer in answers)
			{
				validAnswers.Add(answer.AnswerID, new Object());
				pvs.TotalRecords += answer.Total;
			}

			for (int i = 0; i < end && i < users.Count; i++)
			{
				if (!validAnswers.ContainsKey(voters[users[i]]))
				{
					start--;
					end--;
				}
				else if (i >= start)
					pvs.PollVotes.Add(new PollVote(this, (int) users[i], (string) voters[users[i]]));
			}

			return pvs;
		}

		#endregion
    }
}
